<?php

if (isset($_POST['id'])){//Boton VER
        $dir = "tmp";
        $handle=opendir($dir);

        while (($file = readdir($handle))!==false) {
        //echo "$file <br>";
            //unlink($dir.'/'.$file);
        }
        closedir($handle);

        $file_handle = fopen("conf.txt", "r");
        $line = split(":",fgets($file_handle));

        $hostname= $line[0];
        $db_user= $line[1];
        $db_pass= $line[2];
        $db_name= chop($line[3]);

        fclose($file_handle);
        
       $con = mysql_connect($hostname,$db_user,$db_pass);
       
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        date_default_timezone_set('UTC');
        mysql_select_db($db_name, $con);
        
        $sql = 'SELECT id,nombre,autor,comentario,parametros,informes FROM informe_final WHERE id= "'.$_POST['id'].'";';
        
        $result = mysql_query($sql);
        
        if (mysql_num_rows($result) == 1){
            
            $row = mysql_fetch_array($result);
            
            //$id = $row['id'];
            $nombre = $row['nombre'];
            $autor = $row['autor'];
            $txt = $row['comentario'];
            $informes = explode(',',$row['informes']); //Lista con IDs de informes
            $parametros = explode(',',$row['parametros']); //Lista con parámatros del informe final
            
            require_once('tcpdf/config/lang/spa.php');
            require_once('tcpdf/tcpdf.php');

            // create new PDF document
            $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor($autor);
            $pdf->SetTitle($nombre);
            $pdf->SetSubject('TCPDF Tutorial');
            $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

            // set default header data
            $pdf->SetHeaderData('gos_logo.jpg', PDF_HEADER_LOGO_WIDTH, 'INFORME FINAL'.'', '');

            // set header and footer fonts
            $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
            $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

            //set margins
            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

            //set auto page breaks
            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            //set image scale factor
            $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

            //set some language-dependent strings
            $pdf->setLanguageArray($l);
            
            //PORTADA
            $pdf->AddPage();
            
            $pdf->SetFont('helvetica', '', 8);

            $pdf->SetXY(20, 100);
            $pdf->Write(0, 'Comentario:', '', 0, 'L', true, 0, false, false, 0);
            $pdf->SetXY(20, 105);
            $pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
            }
            //FIN PORTADA
            //
            //INICIO PÁGINAS
            // -------------------------------------------------------------------
            
            foreach ($informes as $id){

            $sql = 'SELECT comentario FROM informe WHERE id='.$id;
            $result = mysql_query($sql);
            $row =  mysql_fetch_array($result);

            // add a page
            $pdf->AddPage();
            
            $pdf->Image('graficos/grafico'.$id.'.png', $x=15, $y=30, $w='', $h='', $type='PNG', $link='', $align='', $resize=true, $dpi=200, $palign='', $ismask=false, $imgmask=false, $border=5, $fitbox=true, $hidden=false, $fitonpage=true);
            
            $pdf->SetFont('helvetica', '', 8);

            $pdf->SetXY(20, 100);
            $pdf->Write(0, 'Comentario:', '', 0, 'L', true, 0, false, false, 0);
            $pdf->SetXY(20, 105);
            $pdf->Write(0, $row['comentario'], '', 0, 'L', true, 0, false, false, 0);
            }
            //FIN PÁGINAS
            
            
            //PDF INFORME FINAL
            $pdf->Output('tmp/'.$nombre.'_'.$autor.'.pdf', 'F');

            echo 'http://localhost/SSOT_SAT/trunk/tmp/'.$nombre.' '.$autor.'.pdf';
                    
}

?>